﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Enyim.Caching;
using System.Web.Mvc;
using SilenthinkContract;
using Web.Manage.Models;
using SilenthinkContract.Domain;

namespace Web.Manage.Controllers
{
    public class EmailController : BaseController
    {
       

        #region Account

        public EmailController(ILogService logService, IBaseService baseService, IExtraService extraService, IStoreService storeService, IOrderService orderService, IGameService gameService, IAuctionService auctionService, IPassportService passportService, IPaymentService paymentService, IMemcachedClient memcachedClient) : base(logService, baseService, extraService, storeService, orderService, gameService, auctionService, passportService, paymentService, memcachedClient)
        {
        }

        public ActionResult Index(string name)
        {
            var model = new EmailControllerModel.Index();
            if (string.IsNullOrEmpty(name))
            {
                model.EmailAccounts = BaseService.GetAllEmailAccount();
            }
            else
            {
                ViewBag.Name = name;
                model.EmailAccounts = BaseService.GetAllEmailAccount().Where(o => o.DisplayName.Contains(name)).ToList();
            }
            return View(model);
        }

        public ActionResult Account(int? param)
        {
            var model = new EmailAccount();
            if (param.HasValue && param.Value > 0)
            {
                model = BaseService.GetEmailAccount(param.Value);
            }
            return View(model);
        }

        [HttpPost]
        public ActionResult Account(int? param, EmailAccount model)
        {
            var dateTime = DateTime.Now;
            var userName = User.Identity.Name;
            if (param.HasValue && param.Value > 0)
            {
                var old = BaseService.GetEmailAccount(param.Value);
                if (TryUpdateModel(old))
                {
                    var result = BaseService.UpdateEmailAccount(old);
                    return result ? Json(HttpStatusCode.OK, "更新成功", true, "/Email") : Json(HttpStatusCode.BadRequest, "更新失败");
                }
            }
            else
            {
                model.CreatedBy = userName;
                model.CreatedOn = dateTime;
                var result = BaseService.CreateEmailAccount(model);
                if (result)
                {
                    return Json(HttpStatusCode.OK, "添加成功", true, "/Email");
                }
            }
            return Json(HttpStatusCode.BadRequest, "失败");
        }

        [HttpPost]
        public ActionResult DeleteAccount(int? param)
        {
            if (param.HasValue && param.Value > 0)
            {
                var result = BaseService.DeleteEmailAccount(param.Value);
                return result ? Json(HttpStatusCode.OK, "删除成功", false, "/Email") : Json(HttpStatusCode.BadRequest, "删除失败");

            }
            return Json(HttpStatusCode.BadRequest, "失败");
        }
        #endregion
        #region Template
        public ActionResult TemplateIndex(string name)
        {
            IList<EmailTemplate> model;
            if (string.IsNullOrEmpty(name))
            {
                model = BaseService.GetAllEmailTemplate();
            }
            else
            {
                ViewBag.Name = name;
                model = BaseService.GetAllEmailTemplate().Where(o => o.Name.Contains(name)).ToList();
            }
            return View(model);
        }
        public ActionResult Template(int? param)
        {
            var model = new EmailTemplate();
            if (param.HasValue && param.Value > 0)
            {
                model = BaseService.GetEmailTemplate(param.Value);
            }
            return View(model);
        }

        [HttpPost,ValidateInput(false)]
        public ActionResult Template(int? param, EmailTemplate model)
        {
            var dateTime = DateTime.Now;
            var userName = User.Identity.Name;
            if (param.HasValue && param.Value > 0)
            {
                var old = BaseService.GetEmailTemplate(param.Value);
                if (TryUpdateModel(old))
                {
                    var result = BaseService.UpdateEmailTemplate(old);
                    return result ? Json(HttpStatusCode.OK, "更新成功", true, "/Email/TemplateIndex") : Json(HttpStatusCode.BadRequest, "更新失败");
                }
            }
            else
            {
                model.CreatedBy = userName;
                model.CreatedOn = dateTime;
                var result = BaseService.CreateEmailTemplate(model);
                if (result)
                {
                    return Json(HttpStatusCode.OK, "添加成功", true, "/Email/TemplateIndex");
                }
            }
            return Json(HttpStatusCode.BadRequest, "失败");
        }

        [HttpPost]
        public ActionResult DeleteTemplate(int? param)
        {
            if (param.HasValue && param.Value > 0)
            {
                var result = BaseService.DeleteEmailTemplate(param.Value);
                return result ? Json(HttpStatusCode.OK, "删除成功", false, "/Email/TemplateIndex") : Json(HttpStatusCode.BadRequest, "删除失败");

            }
            return Json(HttpStatusCode.BadRequest, "失败");
        }
        #endregion
    }
}
